<?php
/*************************************\
    Handshakes Professional (3.3)

            by DZOIC LTD

       Copyright(c) 2005-2007

        auth:zhangchaofeng
\*************************************/

						
include_once(DOC_ROOT."/classes/phpmailer/class.phpmailer.php");

class Mail {

	function Mail(){

		$this->values  = array();
		$this->details = array();
		$this->mail_query = "";
	}//Mail

	function prepare(){
		
		if(!$this->details["mes_id"]){
			$subject = $this->details["subject"];
			$body    = $this->details["body"];
		}
		else{
			include(DOC_ROOT."/include/emails.inc.php");
			$subject = $emails["subject"][$this->details["mes_id"]];
			$body    = $emails["body"][$this->details["mes_id"]];						
		}//else		

		if(is_numeric($this->details["recipient"])){
			$sql_query="SELECT email,fname
						FROM pi_account as a, pi_profile as p
						WHERE a.uid = p.uid
						AND a.uid=".$this->details["recipient"];
			
			$GLOBALS["DB"]->clean();
			$row = $GLOBALS["DB"]->row($sql_query);
			$this->details["recipient_email"] = $row["email"];
			$this->details["recipient_name"]  = $row["fname"];
			//if(!$this->details["notifications"])
			//	$this->details["notifications"]   = $row["notifications"];
			
		}//if
		else{			
			$this->details["recipient_email"] = $this->details["recipient"];			
			$this->details["notifications"]   = 1;
			$this->details["recipient_name"]  = $this->details["recipient_name"];
		}//else
		
		if($this->details["sender"] == "Admin"){
			$sql_query = "SELECT email,fname,lname FROM members WHERE member_type='a'";
			$admin = $GLOBALS["DB"]->row($sql_query);
			$this->details["sender_email"] = $GLOBALS["settings"]["system_mail"];
			$this->details["sender_name"]  = $GLOBALS["settings"]["site_name"];
		}//if
		elseif($this->details["sender"] && is_numeric($this->details["sender"])){
			
			$sql_query="SELECT email,fname,lname
						FROM pi_account as a, pi_profile as p
						WHERE a.uid = p.uid
						AND a.uid=".$this->details["sender"];
			
			$GLOBALS["DB"]->clean();
			$row = $GLOBALS["DB"]->row($sql_query);
			$this->details["sender_email"] = $row["email"];
			$this->details["sender_name"]  = $row["fname"];
			
		}//if
		elseif (strpos($this->details["sender"],"@")){
			$this->details["sender_email"] = $this->details["sender"];
			$this->details["sender_name"]  = $GLOBALS["settings"]["site_name"];
		}
		elseif (!$this->details["sender_email"] || !$this->details["sender_name"]) {
			$this->details["sender_email"] = $GLOBALS["settings"]["system_mail"];
			if(!$this->details["sender_name"])
				$this->details["sender_name"]  = $GLOBALS["settings"]["site_name"];
		}//else
		
		$this->details["subject"] = html_entity_decode(handle_mail_patterns($subject,$this->details));
		$this->details["body"]    = nl2br(handle_mail_patterns($body,$this->details));
		
	}//prepare
	
	function instant(){	
		
		$encoding = "utf-8";
		
		$this->mail = new PHPMailer;
		
		//mailer settings
		$this->mail->Mailer = $GLOBALS["settings"]["mailer"];
		if($GLOBALS["settings"]["mailer"] == "sendmail")
			$this->mail->Sendmail = $GLOBALS["settings"]["mailsetup"];
		elseif($GLOBALS["settings"]["mailer"] == "smtp"){
			$smtp = explode("|",$GLOBALS["settings"]["mailsetup"]);
			$this->mail->Host = $smtp[0];
			$this->mail->Port = $smtp[1];
			if($smtp[2]){
				$this->mail->SMTPAuth = 1;
				$this->mail->Username = $smtp[2];
				$this->mail->Password = $smtp[3];
			}//if
		}//elseif
		
		//mail data
		$this->mail->AddAddress($this->details["recipient_email"],$this->details["recipient_name"]);
		$this->mail->From = $this->details["sender_email"];
		$this->mail->FromName = $this->details["sender_name"];
		$this->mail->CharSet = $encoding;
		$this->mail->ContentType = "text/html";
	
	$this->mail->Body = $this->wrap(html_entity_decode($this->details["body"]),$this->details["recipient_email"]);
		$this->mail->Subject = $this->details["subject"];
		$this->mail->AddReplyTo($this->details["sender_email"]);
		$this->mail->Sender = $this->details["sender_email"];
		$this->mail->send();
	}//instant
	function wrap($body,$email){
	
		$header = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional //EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Perfectinter.net</title></head>
<body style="margin: 0; padding: 0;" dir="ltr">
<table width="98%" border="0" cellspacing="0" cellpadding="40">
	<tr><td bgcolor="#FFF" width="100%" style="font-family: verdana, tahoma, arial, sans-serif;">
	<table cellpadding="0" cellspacing="0" border="0" width="620">
	<tr><td style="background: #014565; color: #FFFFFF; font-weight: bold; font-family:  verdana, tahoma, arial, sans-serif; vertical-align: middle;padding: 4px 8px; font-size: 16px; letter-spacing: -0.03em; text-align: left;">
	<a style="color:#FFFFFF; text-decoration: none;" href="http://www.perfectinter.net"><span style="color:#FFFFFF">Perfectinter.net</span></a></td><td style="background: #014565; color: #FFFFFF; font-weight: bold; font-family:  verdana, tahoma, arial, sans-serif; vertical-align: middle;padding: 4px 8px;font-size: 11px; text-align: right;"></td></tr>
	<tr><td colspan="2" style="background-color: #FFFFFF; border-bottom: 1px solid #014565; border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding: 15px;" valign="top">
		<table width="100%">
			<tr><td width="470px" style="font-size: 14px;" valign="top" align="left">
				<div style="margin-bottom: 15px;">';
				
		$footer = '				<br><br>
			</div>
			</td></tr></table>
	</td></tr>
	<tr><td colspan="2" style="color: #999999; padding: 10px; font-size: 12px; font-family:  verdana,tahoma, arial, sans-serif;">The message was sent to '.$email.' because you are registered on our site.  ® 2012 GIT Global Investments, Inc.
	</td></tr></table>
</td></tr></table>
</body></html>';

		return $header . $body . $footer;
		

	}
	

	function send_external($auto=0){
			
			$this->prepare();		
			//if($this->details["notifications"]){
				$this->instant();
			//}//if
	}//send_external
/*	
	function send_sms($direct=0,$smstype="simple",$url=''){

		if(is_numeric($this->details["recipient"])){
			if($direct)
				$to = $this->details["recipient"];
			else{
				if(!$to) 
					$to = $GLOBALS["DB"]->single("SELECT mobile FROM profiles WHERE mobilev IS NOT NULL AND mem_id = ".$this->details["recipient"]);
				if(!$to)
					$to = $GLOBALS["DB"]->single("SELECT phone FROM profiles WHERE phonev IS NOT NULL AND mem_id = ".$this->details["recipient"]);
				if(!$to){
					$data = $GLOBALS["DB"]->row("SELECT country,phone FROM members_notes WHERE sms = True AND mem_id = ".$this->details["sender"]. " AND  per_id = ".$this->details["recipient"]);
					$data["to"] = ereg_replace("[^0-9]", "", $data["phone"]);
					if(is_numeric($data["to"]))
						$to = $data["country"].$data["to"];
				}
			}

			if(strlen($to) >= 10){
				//send text
				include_once(DOC_ROOT."/clickatell/clickatell_functs.php");
				$delay = $GLOBALS["Get"]->type_val("delay_send","integer");
				$delay_time = "";
				if($delay){
					$date = $GLOBALS["Get"]->val("date");
					if($date[5] == 2)
						$date[3] = $date[3] + 12;
					$date_arr = explode("/",$date[1]);
					//d/m/y input to m-d-y
					$delay_time = $date_arr[1]."-".$date_arr[0]."-".$date_arr[2]."-".$date[3]."-".$date[4];
				}
				//CLEAN TEXT
				
				$text = html_entity_decode($this->details["text"]);
				if(!$text){
						$this->prepare();
						$text = html_entity_decode($this->details["body"]);
					}
				$text = strip_tags($text);		
	
				send_msg($to,"Football",$text,$delay_time,$smstype,$url,$this->details["sender"]);
			}
		}
	}//send_sms

	function send_mass(){
		
		$this->prepare();
		if($this->details["notifications"]){
			
			$sql_query = "INSERT INTO mass_mail(`to`,`subject`,`message`,`headers`)
						  VALUES (':to:',':subject:',':message:',':headers:')";
		
			$values["to"] = $this->details["recipient_email"]."|".$this->details["recipient_name"];
			$values["subject"] = $this->details["subject"];
			$values["message"] = html_entity_decode($this->details["body"]);
			$values["headers"] = $this->details["sender_email"]."|".$this->details["sender_name"];
		
			$this->values = $values;
			$this->execute($sql_query);
		}//if
		
	}//send_mass
	
	function send_internal(){
				
		$mes_id = $this->details["mes_id"];
	
			if($mes_id){
				include(DOC_ROOT."/themes/".$GLOBALS["THEME"]."/system/messages.inc.php");
				$subject = handle_mail_patterns($messages["subject"][$mes_id],$this->details);
				$body    = handle_mail_patterns($messages["body"][$mes_id],$this->details);
			}//if
			else{
				$subject = $this->details["subject"];
				$body    = $this->details["body"];
			}//else
			
			$recipient_member_type = $GLOBALS["Member"]->member_type($this->details["recipient"]);
			
			if(!$this->details["message_type"])
				$this->details["message_type"] = "message";
			if($this->details["invite"]){
			$GLOBALS["DB"]->execute("DELETE FROM internal_invitations WHERE mem_id=".$this->details["sender"]." AND per_id=".$this->details["recipient"]." AND ".$this->details["invite"]."=".$this->details["inv_id"]);
			$GLOBALS["DB"]->execute("INSERT INTO internal_invitations (mem_id,per_id,date,message,subject,".$this->details["invite"].")
															VALUES(".$this->details["sender"].",".$this->details["recipient"].",".time().",'".$body."','".$subject."',".$this->details["inv_id"].")");
			
		}
			if($recipient_member_type != 'a' || $this->details["message_type"] != "message"){
			
				$sql_query = "INSERT INTO internal_mail(mem_id,per_id,subject,message,message_type,special,date)
							  VALUES (:mem_id:,:per_id:,':subject:',':message:',':message_type:',':special:',:time:)";
				
				$values["mem_id"]  = $this->details["recipient"];
				$values["per_id"]  = $this->details["sender"];
				$values["subject"] = $subject;
				$values["message"] = html_entity_decode($body);
				$values["time"]    = time();		
				$values["special"] = $this->details["special"];
				$values["message_type"] = $this->details["message_type"];
			
				$this->values = $values;
				$this->execute($sql_query);
				
			}//if
			elseif($this->details["message_type"] == "message" && $recipient_member_type == 'a'){
				
				$sql_query = "INSERT INTO admin_mail (per_id,subject,message,message_type,date)
							  VALUES (:per_id:,':subject:',':message:',':message_type:',:time:)";
				
				$values["per_id"]  = $this->details["sender"];
				$values["subject"] = $subject;
				$values["message"] = html_entity_decode($body);
				$values["time"]    = time();
				$values["message_type"] = $this->details["message_type"];
			
				$this->values = $values;
				$this->execute($sql_query);
				
			}//else
			
			//process external notice
			switch($this->details["message_type"]){
				case "message":
					$mailbox = "inbox";
					break;
				case "action":
					$mailbox = "actions";
					break;
			
			case "invitation":
					$mailbox = "invitations";
					break;
			
			case "announcement":
				$mailbox = "announcements";
				break;
				default:
					$mailbox = $this->details["message_type"];
				}
			
			$link = URL."/index.php?page=mailbox&amp;section=".$mailbox;
			$to = $this->details["recipient"];
			$sender = $this->details["sender"];
			if($values["special"] && $this->details["message_type"] == "message"){
				//Add club/team link
				$special = explode("|",$values["special"]);
				$type = "From ".$special[0].": ";
				$tlink = '<a href="'.$special[2].'">'.$special[1].'<a>';
			}
			$this->details = array();
			$mail_details = array (
				"mes_id"    => 5,
				"recipient" => $to,
				"message_subject" => $subject,
				"message" => stripslashes($body),
				"mail_link" => $link,
				"type_link" => $tlink,
				"type" => $type,
				"from" => "<a href=".build_link("profile",$sender).">".$GLOBALS["Network"]->network_name($sender)."</a>"
			);

			$this->details = $mail_details;
			$this->send_external(1);
			
			
			return true;
		
		
	}//send_internal
	*/
}//Mail class
$GLOBALS["Mail"] = new Mail;

?>